from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView

from recommand_demo.serializers import CommentSerializer, ReplySerializer
from movie.models import Comment, Reply


class CommentApiView(APIView):
    def get(self, request):
        comment_list = Comment.objects.all()
        serializer = CommentSerializer(instance=comment_list, many=True)
        result = {
            'data': serializer.data,
            'code': 2000,
            'msg': '获取成功'
        }
        return Response(result)
    def post(self,request):
        print(request.data)
        serializer = CommentSerializer(data=request.data,partial=True)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            result = {
                # 'data': serializer.data,
                'code': 2000,
                'msg': '发表成功'
            }
            return Response(result)
        return Response({'msg': '请求失败'}, status=status.HTTP_400_BAD_REQUEST)

# 这里是给已有的回复再新添加回复
@api_view(['POST'])
def get_replycomment(request):
    print(request.data)
    serializer = ReplySerializer(data=request.data, partial=True)
    if serializer.is_valid(raise_exception=True):
        serializer.save()
        # {'reply_author': 1, 'comments_id': 1, 'parent_author': 2, 'reply_content': '这是我回复第一条评论的第五个回复'}
        result = {
            # 'data':serializer.data,
            'code': 2000,
            'msg': '回复成功'
        }
        return Response(result)
    return Response({'msg': '请求失败'}, status=status.HTTP_400_BAD_REQUEST)

